package crp.core.security.domain.services.caller;

import com.qwlabs.cdi.Dispatchable;
import com.qwlabs.security.Caller;
import com.qwlabs.security.CallerAttributeLoader;
import crp.core.security.Callers;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.Nullable;

import javax.enterprise.context.ApplicationScoped;
import java.util.Objects;

@Slf4j
@ApplicationScoped
public class UserIdCallerAttributeLoader implements CallerAttributeLoader<String>, Dispatchable<String> {

    @Override
    public String load(Caller caller) {
        return Callers.isUser(caller) ? caller.id() : null;
    }

    @Override
    public boolean dispatchable(@Nullable String context) {
        return Objects.equals(Callers.ATTRIBUTE_USER_ID, context);
    }
}
